package com.mao.shiro_jsp.service.impl;

import com.mao.shiro_jsp.entity.Perms;
import com.mao.shiro_jsp.entity.User;
import com.mao.shiro_jsp.mapper.UserMapper;
import com.mao.shiro_jsp.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author 小毛同学
 * @version 1.0
 * @data 2021/12/30 10:23
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        // 注册时要进行Md5 + salt + 散列
        String salt = UUID.randomUUID().toString().toUpperCase();
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setSalt(salt);
        user.setPassword(md5Hash.toHex());
        Integer row = userMapper.save(user);
        if (row != 1){
            //抛出异常，注册错误 throw new
            System.out.println("注册错误");
        }
    }


    @Override
    public User findByUserName(String username) {
        User byUserName = userMapper.findByUserName(username);
        if (byUserName == null){
            System.out.println("错误");
        }
       return byUserName;
    }

    @Override
    public User findRolesByUserName(String username) {
        return userMapper.findRolesByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleName(String rolename) {
        return userMapper.findPermsByRoleName(rolename);
    }
}
